#include "../inc/Coordinates.hxx"
#include <cmath>

using namespace std;

Coordinates::Coordinates() {}

Coordinates::Coordinates(double x, double y, double z) {
    this->x = x;
    this->y = y;
    this->z = z;
    updateSphericalCoordinates();
}
/*
Coordinates::Coordinates(double azimuth, double elevation, double radius) {
    this->azimuth = azimuth;
    this->elevation = elevation;
    this->radius = radius;
    updateCartesianCoordinates();
} */
double Coordinates::getDistance() {
    return this->distance;
}

void Coordinates::setDistance(double distance) { 
    this->distance = distance;
    updateCartesianCoordinates();
}

double Coordinates::getAzimuth() {
    return this->azimuth;
}

void Coordinates::setAzimuth(double azimuth) { 
    this->azimuth = azimuth;
    updateCartesianCoordinates();
}

double Coordinates::getElevation() {
    return this->elevation;
}

void Coordinates::setElevation(double elevation) { 
    this->elevation = elevation;
    updateCartesianCoordinates();
}

double Coordinates::getX() {
    return this->x;   
}

void Coordinates::setX(double x) { 
    this->x = x;
    updateSphericalCoordinates();
}

double Coordinates::getY() {
    return this->y;
}

void Coordinates::setY(double y) {
    this->y = y;
    updateSphericalCoordinates();
}

double Coordinates::getZ() {
    return this->z;
}

void Coordinates::setZ(double z) { 
    this->z = z;
    updateSphericalCoordinates();
}

void Coordinates::updateSphericalCoordinates() {
    this->distance = sqrt(x*x + y*y + z*z);
    this->elevation = asin(z/distance);
    this->azimuth = atan2(y,x);   
    if(azimuth < 0) {
       azimuth = 2*M_PI + azimuth;
    } 
}

void Coordinates::updateCartesianCoordinates() {
    this->x = distance*cos(azimuth)*cos(elevation);
    this->y = distance*sin(azimuth)*cos(elevation);
    this->z = distance*sin(elevation);
}

